﻿// --------------------------------
// <copyright file="LocalPlayer.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>22. 11. 2011 23:09:43</date>

// <summary>
//
// </summary>
// --------------------------------

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FITClub.Episode1.Controller
{
    /// <summary>
    /// Local player which uses keyborad.
    /// </summary>
    internal class LocalKeyboardPlayer : APlayer
    {
        private ControlConfig config;
        private KeyboardState LastState;
        private KeyboardState ActualState;

        private int allowReset = 0;
        private PlayerIndex index;
        private int id;

        public LocalKeyboardPlayer(string Name, int playerID)
            : base(Name)
        {
            this.id = playerID;
            if (playerID >= 0 && playerID <= 3)
            {
                index = (PlayerIndex)playerID;
            }
            try
            {
                config = ControlConfig.Load(string.Format("Defaults\\Player {0}", playerID + 1));
            }
            catch (Exception)
            {
                config = new ControlConfig(ControlConfig.DefaultKeySet);
            }
        }

        private void KeyboardAction(Keys key, Action<string> isDownAction, Action<string> releasedAction)
        {
            if (ActualState.IsKeyDown(key))
            {
                isDownAction(this.Name);
            }
            else
            {
                if (LastState.IsKeyDown(key))
                {
                    releasedAction(this.Name);
                }
            }
        }

        public override void Update()
        {
            base.Update();
            LastState = ActualState;
            ActualState = Keyboard.GetState();

            for (int i = 0; i < 6; i++)
            {
                base.SetState(ActualState.IsKeyDown(config[i]), i);
            }
        }

        public override void Dispose()
        {
            config.Save(this.Name);
        }
    }

    /// <summary>
    /// Local player which uses XBOX Gamepad.
    /// </summary>
    internal class LocalXboxPlayer : APlayer
    {
        private GamePadState ActualState;
        //private int allowReset = 0;
        private PlayerIndex index;
        private int id;

        public LocalXboxPlayer(string Name, int playerID)
            : base(Name)
        {
            this.id = playerID;
            if (playerID >= 0 && playerID <= 3)
            {
                index = (PlayerIndex)playerID;
            }
        }

        public override void Update()
        {
            base.Update();
            this.ActualState = GamePad.GetState(index);
            if (ActualState.IsConnected == false) return;

            bool state = this.ActualState.IsButtonDown(Buttons.DPadLeft) || this.ActualState.IsButtonDown(Buttons.LeftThumbstickLeft);
            base.SetState(state, 1);

            state = this.ActualState.IsButtonDown(Buttons.DPadRight) || this.ActualState.IsButtonDown(Buttons.LeftThumbstickRight);
            base.SetState(state, 3);

            state = this.ActualState.IsButtonDown(Buttons.DPadUp) || this.ActualState.IsButtonDown(Buttons.LeftThumbstickUp);
            base.SetState(state, 0);

            state = this.ActualState.IsButtonDown(Buttons.DPadDown) || this.ActualState.IsButtonDown(Buttons.LeftThumbstickDown);
            base.SetState(state, 2);

            state = this.ActualState.IsButtonDown(Buttons.A) || this.ActualState.IsButtonDown(Buttons.LeftTrigger);
            base.SetState(state, 4);

            state = this.ActualState.IsButtonDown(Buttons.B) || this.ActualState.IsButtonDown(Buttons.LeftTrigger);
            base.SetState(state, 5);
        }

        public override void Dispose()
        {
        }
    }
}